<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;

use App\Middleware\JwtMiddleware;
use App\Model\Collect;
use App\Model\Information;
use App\Model\Order;
use App\Model\User;
use App\Resource\CollectCollection;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;

#[Controller]
class CollectController
{
    #[Inject]
    protected RequestInterface $request;

    #[Middleware(JwtMiddleware::class)]
    #[RequestMapping(path: "", methods: "get")]
    public function list()
    {
        /** @var User $user */
        $user = auth()->user();
        $list = $user->collect();

        $list->with(['information' => function ($q) {
            $q->with(['imgs', 'ad', 'cp', 'pc', 'freeRider', 'houseSales', 'jobWanted', 'publicGood', 'recruitment', 'rent', 'solicit']);
        }]);
        $list = $list->paginate((integer)$this->request->input('page_num'));

        $data = [
            'list' => (new CollectCollection($list))->toListApi(),
            'count' => $list->total(),
            'count_page' => $list->lastPage(),
            'page_num' => $list->perPage(),
        ];
        return self::jsonResponse(200, 'success', $data);
    }

    #[Middleware(JwtMiddleware::class)]
    #[RequestMapping(path: "collect", methods: "post")]
    /**
     * 加入/取消收藏
     * @return false|string
     * @throws \Exception
     */
    public function collect()
    {
        $id = $this->request->input('id');
        $information = Information::getById($id);
        if (!$information) {
            return self::jsonResponse(404, '数据不存在');
        }

        /** @var User $user */
        $user = auth()->user();
        $collect = $user->collect()->where('information_id', $id)->first();

        //如果已加入收藏则取消收藏
        if ($collect) {
            $collect->delete();
        } else {
            //如果不在没有收藏过则加入收藏
            $collect = $user->collect()->make();
            $collect->information_id = $id;
            $collect->save();
        }

        return self::jsonResponse(200, 'success');
    }

    public static function jsonResponse(int $status = 1, $message = 'success', $data = null, $headerStatus = 200)
    {
        if (is_array($message)) {
            $data = $message;
            $message = 'success';
        }
        if (empty($data)) {
            $data = new class {
            };
        }

        return json_encode(['status' => $status, 'message' => $message, 'data' => $data]);

//        return response()->json(
//            ['status' => $status, 'message' => $message, 'data' => $data],
//            $headerStatus,
//            [],
//            JSON_UNESCAPED_UNICODE
//        );
    }
}
